﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;


namespace HRM
{
    public partial class frmGradeDetailList : FormBase
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        string _OrderBy = "";
        string _OrderDirection = "";
        int _GDID = 0;
        GradeDetailClient _wcfGD;
        RoleRightInfo listRole;
        decimal _MinBasicSalary = 0;
        bool _IsSelect = false;

        int _GradeID = 0;
        public int GradeID
        {
            get { return _GradeID; }
            set { _GradeID = value; }
        }
        string _GradeName;
        public string GradeName
        {
            get { return _GradeName; }
            set { _GradeName = value; }
        }

        int _LevelID = 0;
        public int LevelID
        {
            get { return _LevelID; }
            set { _LevelID = value; }
        }
        string _LevelName;
        public string LevelName
        {
            get { return _LevelName; }
            set { _LevelName = value; }
        }
        decimal _Amount = 0;

        public decimal Amount
        {
            get { return _Amount; }
            set { _Amount = value; }
        }
        int _CountMonth = 0;
        public int CountMonth
        {
            get { return _CountMonth; }
            set { _CountMonth = value; }
        }


        public override void Show_Form()
        {
            this.Show();
        }

        public frmGradeDetailList()
        {
            InitializeComponent();
            Init_Control();
        }

        public frmGradeDetailList(bool IsSelect = false)
        {
            InitializeComponent();
            _IsSelect = IsSelect;
            Init_Control();
        }

        protected override void Init_Control()
        {
            // LoadLanguage();
            
            _wcfGD = new GradeDetailClient(ProxyConfig.GetBinding(), ProxyConfig.GradeDetail);
            BranchClient _wcfBranch = Service.Branch();
            _MinBasicSalary = _wcfBranch.Branch_GetInfo(Program.intBranchID).MinBasicSalary;
            listRole = UIParamater.Quyen_NgachBac;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole._delete)
                btnDel.Enabled = false;
            if (!listRole.Update)
            {
                this.btnEdit.Image = global::HRM.Properties.Resources.app;
                btnEdit.Text = "xem";
            }
            this.KeyPreview = true;
            BuildVoucherGrid();
            loadList();

            //
            base.Init_Control();
        }
             

        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = this.TabText = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }

        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "GradeDetail_ID", "", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "GradeDetail_GradeID", "", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "GradeDetail_LevelID", "", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "Grade_Name", "Ngạch chức danh", "", 150, true, true);
            UISetting.AddColumn(ref dgvList, "Level_Name", "Bậc", "", 150, true, true);
            UISetting.AddColumn(ref dgvList, "GradeDetail_Amount", "Hệ số lương", "R", 120, true, true);
            //UISetting.AddColumn(ref dgvList, "MucLuong", "Mức lương", "R", 120, true, true);
            UISetting.AddColumn(ref dgvList, "GradeDetail_Month", "Số tháng nâng lương", "R", 140, true, true);
            UISetting.AddColumn(ref dgvList, "GradeDetail_BranchCode", "", "", 1, false, true);
        }

        private void loadList()
        {
            DataSet _ds = _wcfGD.GradeDetail_GetList(out itemCount, Program.strBranchBHXH, currPage, pageSize, UISetting.GetKeyWord(txtKeyWord.Text), _OrderBy, _OrderDirection);
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();

            dgvList.Rows.Clear();
            for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["GradeDetail_ID"].Value = _ds.Tables[0].Rows[i]["GradeDetail_ID"].ToString();
                dgvList.Rows[i].Cells["GradeDetail_GradeID"].Value = _ds.Tables[0].Rows[i]["GradeDetail_GradeID"].ToString();
                dgvList.Rows[i].Cells["GradeDetail_LevelID"].Value = _ds.Tables[0].Rows[i]["GradeDetail_LevelID"].ToString();
                //dgvList.Rows[i].Cells["RowNumber"].Value = _ds.Tables[0].Rows[i]["RowNumber"].ToString();
                dgvList.Rows[i].Cells["Grade_Name"].Value = _ds.Tables[0].Rows[i]["Grade_Name"].ToString();
                dgvList.Rows[i].Cells["Level_Name"].Value = _ds.Tables[0].Rows[i]["Level_Name"].ToString();
                dgvList.Rows[i].Cells["GradeDetail_Amount"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["GradeDetail_Amount"]).ToString(UIParamater.Cham2So);
                dgvList.Rows[i].Cells["GradeDetail_Month"].Value = Convert.ToInt32(_ds.Tables[0].Rows[i]["GradeDetail_Month"]).ToString();
                //dgvList.Rows[i].Cells["MucLuong"].Value = (Convert.ToDecimal(_ds.Tables[0].Rows[i]["GradeDetail_Amount"]) * _MinBasicSalary).ToString("#,##0");
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                return;
            }
            Form f = new frmGradeDetailInfo(0, listRole);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                loadList();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {

                    _GDID = Convert.ToInt32(dgvList.CurrentRow.Cells["GradeDetail_ID"].Value);
                    Form f = new frmGradeDetailInfo(_GDID, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        loadList();
                    }
                }
                else
                    Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("khongcoquyenxem"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    return;
                }
                _GDID = Convert.ToInt32(dgvList.CurrentRow.Cells["GradeDetail_ID"].Value);
                if (_wcfGD.GradeDetail_Using(_GDID) != 0)
                {
                    MessageBox.Show("Ngạch bậc này đang được sử dụng, không thể xóa!", UpdateUI.GetString("canhbao"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UpdateUI.GetString("canhbao"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (_wcfGD.GradeDetail_Delete(_GDID))
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        loadList();
                    }
                    else
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    }

                }
            }

        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            loadList();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;

            loadList();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;

            loadList();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage > 0)
                currPage = totalPage - 1;

            loadList();

        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (_IsSelect)
            {
                _GradeID = Convert.ToInt32(dgvList.SelectedRows[0].Cells["GradeDetail_GradeID"].Value);
                _GradeName = dgvList.SelectedRows[0].Cells["Grade_Name"].Value.ToString();
                _LevelID = Convert.ToInt32(dgvList.SelectedRows[0].Cells["GradeDetail_LevelID"].Value);
                _LevelName = dgvList.SelectedRows[0].Cells["Level_Name"].Value.ToString();
                _Amount = Convert.ToDecimal(dgvList.SelectedRows[0].Cells["GradeDetail_Amount"].Value);
                _CountMonth = Convert.ToInt32(dgvList.SelectedRows[0].Cells["GradeDetail_Month"].Value);
                this.DialogResult = DialogResult.OK;
            }
            else
                btnEdit_Click(null, null);
        }

        private void frmGradeList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
            }
        }

        private void btnNewPopUp_Click(object sender, EventArgs e)
        {
            btnNew_Click(null, null);
        }

        private void btnEditPopUp_Click(object sender, EventArgs e)
        {
            btnEdit_Click(null, null);
        }

        private void btnDelPopUp_Click(object sender, EventArgs e)
        {
            btnDel_Click(null, null);
        }

        private void dgvList_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (dgvList.SelectedRows.Count > 0)
                {
                    mnuPopup.Enabled = true;
                    btnNewPopUp.Enabled = true;
                    btnEditPopUp.Enabled = true;
                    btnDelPopUp.Enabled = true;
                }
            }
        }

        private void frmGradeList_Activated(object sender, EventArgs e)
        {
            // LoadLanguage();
            if (dgvList.Columns.Count > 0)
            {
                dgvList.Columns["Grade_Name"].HeaderText = "Ngạch chức danh";
                dgvList.Columns["Level_Name"].HeaderText = "Bậc";
                dgvList.Columns["GradeDetail_Amount"].HeaderText = "Hệ số lương";
                //dgvList.Columns["MucLuong"].HeaderText = "Mức lương";
            }
        }

        private void dgvList_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            _OrderBy = dgvList.SortedColumn.Name;
            switch (dgvList.SortOrder.ToString())
            {
                case "Ascending":
                    _OrderDirection = "ASC";
                    break;
                case "Descending":
                    _OrderDirection = "DESC";
                    break;
            }
            loadList();
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            loadList();

        }
    }
}
